package com.last1mile.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.last1mile.dao.interfaces.MerServeDao;
import com.last1mile.dao.interfaces.ServeDestDao;
import com.last1mile.dao.interfaces.StreetDao;
import com.last1mile.dao.interfaces.SubscribeDao;
import com.last1mile.model.Serve;
import com.last1mile.pojo.MerServeDO;
import com.last1mile.pojo.ServeDestDO;
import com.last1mile.pojo.StreetDO;
import com.last1mile.pojo.SubscribeDO;
import com.last1mile.service.ServeService;

@Component("serveServiceImpl")
public class ServeServiceImpl implements ServeService {

    @Autowired
    @Qualifier("merServeDao")
    private MerServeDao  merServeDao;
    
    @Autowired
    @Qualifier("serveDestDao")
    private ServeDestDao serveDestDao;

    @Autowired
    @Qualifier("subscribeDao")
    private SubscribeDao subscribeDao;
    
    @Autowired
    @Qualifier("streetDao")
    private StreetDao streetDao;

    @Override
    public void createMerServe(Serve serve) {
        if (serve != null) {
            MerServeDO merServeDo = new MerServeDO();
            merServeDo.setMerId(serve.getMerId());
            merServeDo.setServeTitle(serve.getServeTitle());
            merServeDo.setServeDesc(serve.getServeDesc());
            merServeDo.setServeCate(serve.getServeCate());
            merServeDo.setPriceAmt(serve.getPriceAmt());
            merServeDo.setPriceCuy(serve.getPriceCuy());
            merServeDo.setPriceUnit(serve.getPriceUnit());
            merServeDo.setPriceDesc(serve.getPriceDesc());
            Integer serveId = this.merServeDao.insertServe(merServeDo);
            
            if(serveId != null){
                // 处理ServeDest的数据
                String[] addrState = serve.getAddrState();
                String[] addrCity = serve.getAddrCity();
                String[] addrDistrict = serve.getAddrDistrict();
                String[] addrStreet = serve.getAddrStreet();
                List<ServeDestDO> destList = new ArrayList<ServeDestDO>();
                for(int i = 0; i < addrState.length; i++){
                    ServeDestDO dest = new ServeDestDO();
                    if((StringUtils.isNotBlank(addrState[i]))&&(!StringUtils.equals(addrState[i], "null"))){
                        dest.setAddrState(addrState[i]);
                    }
                    if((StringUtils.isNotBlank(addrCity[i]))&&(!StringUtils.equals(addrCity[i], "null"))){
                        dest.setAddrCity(addrCity[i]);
                    }
                    if((StringUtils.isNotBlank(addrDistrict[i]))&&(!StringUtils.equals(addrDistrict[i], "null"))){
                        dest.setAddrDistrict(addrDistrict[i]);
                    }
                    
                    if((StringUtils.isNotBlank(addrStreet[i]))&&(!StringUtils.equals(addrStreet[i], "null"))){
                    	Integer streetId = null;
                    	 // 处理Street的数据,将Street的 东方郡  这样的CnName转化成code
                    	StreetDO streetDo = this.streetDao.selectByStreetUpIdAndCnName(dest.getAddrDistrict(), addrStreet[i]);
                    	if(streetDo == null){
                    		streetDo = new StreetDO();
                    		streetDo.setStreetUpId(dest.getAddrDistrict());
                    		streetDo.setCnName(addrStreet[i]);
                    		streetId = this.streetDao.insert(streetDo);
                    	}else{
                    		streetId = streetDo.getId();
                    	}
                        dest.setAddrStreet(streetId+"");
                    }
                    dest.setServeId(serveId);
                    destList.add(dest);
                }
                this.serveDestDao.insertServeDestList(destList);
            }
        }
    }

    @Override
    public void createSubscribe(Integer perId, Integer merId, Integer serveId) {
        if ((perId != null) && (merId != null) && (serveId != null)) {
            SubscribeDO subscribeDo = new SubscribeDO();
            subscribeDo.setPerId(perId);
            subscribeDo.setMerId(merId);
            subscribeDo.setMerServeId(serveId);
            this.subscribeDao.inserSubscribe(subscribeDo);
        }
    }

    @Override
    public List<MerServeDO> queryServesByMerId(Integer merId) {
        if (merId != null) {
            return this.merServeDao.queryServeByMerId(merId);
        } else {
            return null;
        }
    }

    @Override
    public List<MerServeDO> queryServeByAddrExceptUser(String addrCountry, String addrState, String addrCity,
                                                       String addrDistrict, Integer merId) {
        List<MerServeDO> resultServeList = null;
        if (StringUtils.isNotBlank(addrCountry) || StringUtils.isNotBlank(addrState)
            || StringUtils.isNotBlank(addrCity) || StringUtils.isNotBlank(addrDistrict)) {
            MerServeDO merServeDo = new MerServeDO();
            merServeDo.setAddrCountry(addrCountry);
            merServeDo.setAddrState(addrState);
            merServeDo.setAddrCity(addrCity);
            merServeDo.setAddrDistrict(addrDistrict);

            if (merId != null) {
                merServeDo.setMerId(merId);
            }

            resultServeList = this.merServeDao.queryServeByAddrExceptUser(merServeDo);
        }

        return resultServeList;
    }

    @Override
    public MerServeDO queryServeById(Integer serveId) {
        if (serveId != null) {
            return this.merServeDao.queryServeById(serveId);
        } else {
            return null;
        }
    }

    @Override
    public Integer countMerBeSubscribed(Integer merId) {
        if (merId != null) {
            SubscribeDO subscribeDo = new SubscribeDO();
            subscribeDo.setMerId(merId);
            return this.subscribeDao.countSubscribe(subscribeDo);
        } else {
            return null;
        }
    }

    @Override
    public Integer countServeBeSubscribed(Integer serveId) {
        if (serveId != null) {
            SubscribeDO subscribeDo = new SubscribeDO();
            subscribeDo.setMerServeId(serveId);
            return this.subscribeDao.countSubscribe(subscribeDo);
        } else {
            return null;
        }
    }

    @Override
    public Integer countSubscribe(Integer perId, Integer merId, Integer serveId) {
        if ((perId != null) && (merId != null) && (serveId != null)) {
            SubscribeDO subscribeDo = new SubscribeDO();
            subscribeDo.setPerId(perId);
            subscribeDo.setMerId(merId);
            subscribeDo.setMerServeId(serveId);
            return this.subscribeDao.countSubscribe(subscribeDo);
        } else {
            return null;
        }
    }

    @Override
    public Integer countPerSubscribe(Integer perId) {
        if (perId != null) {
            SubscribeDO subscribeDo = new SubscribeDO();
            subscribeDo.setPerId(perId);
            return this.subscribeDao.countSubscribe(subscribeDo);
        } else {
            return null;
        }
    }

	@Override
	public List<MerServeDO> queryServesByPerId(Integer perId) {
	    List<MerServeDO> merServeList = new ArrayList<MerServeDO>();
		List<Integer> ids = this.subscribeDao.queryMerServeIdByPerId(perId);
		if(!CollectionUtils.isEmpty(ids)){
		    merServeList = this.merServeDao.queryServesByIdList(ids);
		}
		return merServeList;
	}

}
